package javaweb.controller;

import javaweb.anno.Log;
import javaweb.pojo.Result;
import javaweb.pojo.Student;
import javaweb.pojo.StudentPageResult;
import javaweb.pojo.StudentQueryParam;
import javaweb.sevice.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /*
    * 学员信息分页查询
    * */
    @GetMapping
    public Result findAllStudent(StudentQueryParam studentQueryParam){
        log.info("学生分页查询：{}", studentQueryParam);
        StudentPageResult studentPageResult =  studentService.findAllStudent(studentQueryParam);
        return Result.success(studentPageResult);
    }

    /*
    * 添加学员信息
    * */
    @Log
    @PostMapping
    public Result addStudent(@RequestBody Student student){
        log.info("新增学员信息为：{}", student);
        studentService.addStudent(student);
        return Result.success();
    }

    /*
    * 根据id查询学员
    * */
    @GetMapping("/{id}")
    public Result findStudentById(@PathVariable int id){
        log.info("查询的学员id为：{}",id);
        Student student = studentService.findStudentById(id);
        return Result.success(student);
    }

    /*
    * 修改学员信息
    * */
    @Log
    @PutMapping
    public Result updateStudent(@RequestBody Student student){
        log.info("修改学员信息为：{}",student);
        studentService.updateStudent(student);
        return Result.success();
    }

    /*
    * 删除学员信息
    * */
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteStudent(@PathVariable Integer[] ids){
        log.info("要删除的学生id为：{}", Arrays.toString(ids));
        studentService.deleteStudentById(ids);
        return Result.success();
    }

    /*
    * 违纪处理
    * */
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result disciplinaryStudent(@PathVariable Integer id,
                                      @PathVariable Integer score){

        log.info("违纪处理接收到的路径参数为：{}，{}",id,score);
        studentService.disciplinaryStudent(id, score);
        return Result.success();
    }
}
